﻿using Authentication.Model.Interface;
using Common.Contract.Authentication.Record;
using Common.Contract.Authentication.Request;
using Common.Contract.Authentication.Response;
using Common.Contract.Paging;
using Common.Contract.SystemConfig.Record;
using Common.Service.Command;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Authentication.Core.ServiceCommand
{
    public class RetrieveEditDaoRequestDetailServiceCommand : ProviderServiceCommand<IProviderFactory, RetrieveEditDaoRequestDetailRequest, RetrieveEditDaoRequestDetailResponse>
    {

        public int pageSize = 10, pageIndex = 0, totalRecord = 0;
        public string TextSearch;
        public FilterRecord Filter = null;
        private UserRecord currentUser;
        public RetrieveEditDaoRequestDetailServiceCommand(IProviderFactory providers) : base(providers) { }
        protected override void Validate(RetrieveEditDaoRequestDetailRequest request)
        {
            if (request == null) throw new ArgumentNullException("request");
            if (!request.Header.ApplicationKey.HasValue || request.Header.ApplicationKey == Guid.Empty)
            {
                throw new ArgumentException("ApplicationKey must be set", "request");
            }
            if (request.CurrentUser == null)
            {
                throw new ArgumentException("User record must be set", "request");
            }
            else
            {
                currentUser = request.CurrentUser;
            }
            if (Request.CurrentUser == null)
            {
                throw new ArgumentException("Dao must be set", "request");
            }
            Filter = Request.Filter;
        }

        protected override bool OnExecute(RetrieveEditDaoRequestDetailResponse response)
        {
            bool isSuccess = true;
            string errorMessage = string.Empty;
            int pageSize = 0, pageIndex = 0;
            var applicationKey = Request.Header.ApplicationKey;
            try
            {
                if (Request.Page != null)
                {
                    pageIndex = Request.Page.PageIndex ?? 0;
                    pageSize = Request.Page.PageSize ?? 0;
                }
                var listUser = Providers.EditDao.RetrieveRequestByRequestId(Request.RequestId, currentUser);
                var daoCode16 = Providers.SaleCode.RetrieveDaoCodeByDao(listUser.Dao, currentUser);
                var daoEdit = Providers.EditDao.RetrieveRequestDetailByRequestId(Request.RequestId, currentUser);
                if (listUser == null)
                {
                    response.ErrorMessage = string.Format("Can not found user with Dao={0}", currentUser.Dao);
                    response.IsSuccess = false;
                }
                List<EditDaoRequestDetailRecord> records = new List<EditDaoRequestDetailRecord>();
                records.Add(new EditDaoRequestDetailRecord
                {
                    Cif = daoCode16.Cif,
                    Dao = daoCode16.Dao,
                    DaoName = daoCode16.DaoName,
                    DeptParent = daoCode16.DeptParent,
                    Division = daoCode16.Division,
                    PositionName = daoCode16.DeliveryPoint,
                    StaffId = daoCode16.StaffId,
                    Type = "Hiện tại"
                });
                records.Add(new EditDaoRequestDetailRecord
                {
                    Cif = daoEdit.Cif,
                    Dao = daoEdit.Dao,
                    DaoName = daoEdit.DaoName,
                    DeptParent = daoEdit.DeptParent,
                    Division = daoEdit.Division,
                    PositionName = daoEdit.DeliveryPoint,
                    StaffId = daoEdit.StaffId,
                    Type = "Chỉnh sửa"
                });
                response.Records = records;
                response.Page = new PageRecord
                {
                    PageIndex = pageIndex,
                    PageSize = pageSize,
                    TotalRecords = records.Count(),
                };
            }
            catch (Exception e)
            {
                Console.Write(e.Message);
                response.ErrorMessage = string.Format("Can not found user with Dao={0}", Request.CurrentUser.Dao);
                response.IsSuccess = false;
            }
            return isSuccess;
        }
    }
}
